package com.tian.view.draw.fermode

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.PorterDuff
import android.graphics.PorterDuffXfermode
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import com.tian.util.view.DisplayUtil

/**
 * @author tiansongtao
 * @date 2023/1/6 13:35
 *
 * [Paint.setXfermode] 两张图片怎么融合
 *
 * PorterDuffXfermode:
 */
class PorterDuffXfermodeView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) :
        View(context, attrs, defStyleAttr) {

    private val TAG = "tiansongtao"

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context) : this(context, null)

    init {
        setLayerType(LAYER_TYPE_SOFTWARE, null)
    }

    private val w = 80f
    private val h = 80f

    private val paint = Paint()

    private val textPaint = Paint().apply {
        color = Color.BLACK
        style = Paint.Style.FILL_AND_STROKE
        textSize = DisplayUtil.spToPx(10f)
        isAntiAlias = true
        textAlign = Paint.Align.CENTER
    }


    private val dst: Bitmap by lazy {
        val bitmap = Bitmap.createBitmap(w.toInt(), h.toInt(), Bitmap.Config.ARGB_8888)
        val c = Canvas(bitmap)
        val p = Paint(Paint.ANTI_ALIAS_FLAG)
        p.color = 0xFFFFCC44.toInt()
        c.drawOval(RectF(0f, 0f, w, h), p)
        bitmap
    }

    private val src: Bitmap by lazy {
        val bitmap = Bitmap.createBitmap(w.toInt(), h.toInt(), Bitmap.Config.ARGB_8888)
        val c = Canvas(bitmap)
        val p = Paint(Paint.ANTI_ALIAS_FLAG)
        p.color = 0xFF66AAFF.toInt()
        c.drawRect(0f, 0f, w, h, p)
        bitmap
    }


    private val offsetY = 230f


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), offsetY.toInt() * 3)
    }

    private val num = 6f

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val offsetX = width / num
        canvas.save()
        fermodeDraw(canvas, PorterDuff.Mode.CLEAR)
        canvas.translate(offsetX, 0f)
        fermodeDraw(canvas, PorterDuff.Mode.SRC)
        canvas.translate(offsetX, 0f)
        fermodeDraw(canvas, PorterDuff.Mode.DST)
        canvas.translate(offsetX, 0f)
        fermodeDraw(canvas, PorterDuff.Mode.SRC_OVER)
        canvas.translate(offsetX, 0f)
        fermodeDraw(canvas, PorterDuff.Mode.DST_OVER)
        canvas.translate(offsetX, 0f)
        fermodeDraw(canvas, PorterDuff.Mode.SRC_IN)
        canvas.restore()

        canvas.save()
        canvas.translate(0f, offsetY)
        fermodeDraw(canvas, PorterDuff.Mode.DST_IN)
        canvas.translate(offsetX, 0f)
        fermodeDraw(canvas, PorterDuff.Mode.SRC_OUT)
        canvas.translate(offsetX, 0f)
        fermodeDraw(canvas, PorterDuff.Mode.DST_OUT)
        canvas.translate(offsetX, 0f)
        fermodeDraw(canvas, PorterDuff.Mode.SRC_ATOP)
        canvas.translate(offsetX, 0f)
        fermodeDraw(canvas, PorterDuff.Mode.DST_ATOP)
        canvas.translate(offsetX, 0f)
        fermodeDraw(canvas, PorterDuff.Mode.XOR)
        canvas.restore()

        canvas.save()
        canvas.translate(0f, offsetY * 2)
        fermodeDraw(canvas, PorterDuff.Mode.DARKEN)
        canvas.translate(offsetX, 0f)
        fermodeDraw(canvas, PorterDuff.Mode.MULTIPLY)
        canvas.translate(offsetX, 0f)
        fermodeDraw(canvas, PorterDuff.Mode.LIGHTEN)
        canvas.translate(offsetX, 0f)
        fermodeDraw(canvas, PorterDuff.Mode.SCREEN)
        canvas.translate(offsetX, 0f)
        fermodeDraw(canvas, PorterDuff.Mode.ADD)
        canvas.translate(offsetX, 0f)
        fermodeDraw(canvas, PorterDuff.Mode.OVERLAY)
        canvas.restore()
    }


    private fun fermodeDraw(canvas: Canvas, model: PorterDuff.Mode) {
        val ww = width / num
        canvas.drawText("${model.name}", ww / 2f, 40f, textPaint)
        val layerId = canvas.saveLayer(0f, 0f, ww, 220f, null)
        canvas.drawBitmap(dst, ww / 2f - (w / 4 * 3), 60f, paint)
        paint.xfermode = PorterDuffXfermode(model)
        canvas.drawBitmap(src, ww / 2f - (w / 4), 60f + (h / 2), paint)
        paint.xfermode = null
        canvas.restoreToCount(layerId)
    }


}